﻿@using eLoan.Models;
@model LoanDetailsModel
@{
    ViewBag.Title = "Loan Details";
}

<div class="row wrapper border-bottom white-bg page-heading">
    <div class="col-lg-10">
        <h2>Loan Details</h2>
        <ol class="breadcrumb">
            <li>
                Funding
            </li>
            <li>
                <a href="@Url.Action("Loans", "Funding")">Loans</a>
            </li>
            <li class="active">
                <strong>Loan Details</strong>
            </li>
        </ol>
    </div>
    <div class="col-lg-2"></div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div id="divLoanDetails" class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-left">
                        <h3>Loan Details</h3>
                    </div>
                </div>
                <div class="ibox-content">
                    <table class="table table-bordered" style="width:100%">
                        <thead class="k-grid-header">
                            <tr>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.BorrowerName)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.AgentName)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.BranchName)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.ProceedTypeName)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.Term)</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <span>@Html.DisplayFor(m => m.Loan.BorrowerName)</span>
                                </td>
                                <td>
                                    <span>@Html.DisplayFor(m => m.Loan.AgentName)</span>
                                </td>
                                <td>
                                    <span>@Html.DisplayFor(m => m.Loan.BranchName)</span>
                                </td>
                                <td>
                                    <span>@Html.DisplayFor(m => m.Loan.ProceedTypeName)</span>
                                </td>
                                <td>
                                    <span>@Html.DisplayFor(m => m.Loan.Term)</span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <table class="table table-bordered" style="width: 100%">
                        <thead class="k-grid-header">
                            <tr>

                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.DocumentNo)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.LoanAmount)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.LoanInterest)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.LoanInterestAmount)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.Receivable)</span>
                                </th>
                            </tr>
                        </thead>
                        <tr>
                            <td>
                                <span>@Html.DisplayFor(m => m.Loan.DocumentNo)</span>
                                @Html.HiddenFor(m => m.Loan.DocumentNo, new { @id = "txtDocumentNo" })
                            </td>
                            <td>
                                <span>@Html.ValueFor(m => m.Loan.LoanAmount, "{0:#,###,##0.00}")</span>
                            </td>
                            <td>
                                <span>@Html.DisplayFor(m => m.Loan.LoanInterest)</span>
                            </td>
                            <td>
                                <span>@Html.ValueFor(m => m.Loan.LoanInterestAmount, "{0:#,###,##0.00}")</span>
                            </td>
                            <td>
                                <span>@Html.ValueFor(m => m.Loan.Receivable, "{0:#,###,##0.00}")</span>
                            </td>
                        </tr>
                    </table>
                    <table class="table table-bordered" style="width:100%">
                        <thead class="k-grid-header">
                            <tr>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.TotalDeduction)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.ChequeAmount)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.TotalPayment)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.OutstandingBalance)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span>@Html.LabelFor(m => m.Loan.LoanStatus)</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.TotalDeduction, "{0:#,###,##0.00}")</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.ChequeAmount, "{0:#,###,##0.00}")</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.TotalPayment, "{0:#,###,##0.00}")</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.OutstandingBalance, "{0:#,###,##0.00}")</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.LoanStatus)</span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <table class="table table-bordered">
                        <thead class="k-grid-header">
                            <tr>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.PaymentStartDate)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.PaymentEndDate)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.PaymentTypeName)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.EncodedBy)</span>
                                </th>
                                <th class="k-header" style="width:20%">
                                    <span class="k-link">@Html.LabelFor(m => m.Loan.DocumentDate)</span>
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.PaymentStartDate, "{0:MMM/dd/yyyy}")</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.PaymentEndDate, "{0:MMM/dd/yyyy}")</span>
                                </td>
                                <td>
                                    @if (Model.IsAdmin)
                                    {
                                        @Html.DropDownListFor(m => m.Loan.PaymentTypeID, new SelectList(Model.PaymentTypes, "Value", "Text", Model.Loan.PaymentTypeID), new { @id = "ddlPaymentType", @class = "single-line", @onchange = "SaveDetails()" })
                                    }
                                    else
                                    {
                                        <span>@Html.ValueFor(m => m.Loan.PaymentTypeName)</span>
                                    }
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.EncodedBy)</span>
                                </td>
                                <td>
                                    <span>@Html.ValueFor(m => m.Loan.DocumentDate, "{0:MMM/dd/yyyy}")</span>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        
        <div id="divDeductions" class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-left">
                        <h3>Deductions</h3>
                    </div>
                    <div class="pull-right">
                        <h3>Total deduction: @Html.ValueFor(m => m.Loan.TotalDeduction, "{0:#,###,##0.00}")</h3>
                    </div>
                </div>
                <div class="ibox-title">
                    <table>
                        <tr>
                            <td style="@Model.AddDeductionStyle"><a href="@Url.Action("AddEdit", "LoanDeduction", new { documentNo = Model.Loan.DocumentNo, command = "Add" })" class="btn btn-primary btn-sm" style="margin-right:10px;">Add new deductions</a></td>
                            <td style="@Model.EditDeductionStyle"><a href="@Url.Action("AddEdit", "LoanDeduction", new { documentNo = Model.Loan.DocumentNo, command = "Edit" })" class="btn btn-primary btn-sm">Edit existing deductions</a></td>
                            <td></td>
                        </tr>
                    </table>
                </div>
                <div class="ibox-content">
                    <div class="divScrollDeductions">
                        <table class="table table-bordered">
                            <thead class="k-grid-header">
                                <tr>
                                    <th>
                                        <span>Charge Type</span>
                                    </th>
                                    <th>
                                        <span>Amount</span>
                                    </th>
                                    <td colspan="2"></td>
                                </tr>
                            </thead>
                            <tbody>
                                @for (var i = 0; i < @Model.LoanDeductions.Count(); i++)
                                {
                                    <tr>
                                        <td>@Html.DisplayFor(m => Model.LoanDeductions[i].ChargeTypeName)</td>
                                        <td style="text-align:right">@Html.ValueFor(m => Model.LoanDeductions[i].Amount, "{0:#,###,##0.00}")</td>
                                        @*<td>@Html.ActionLink("Delete", "Delete", "LoanDeduction", null, null, null, new { id = Model.LoanDeductions[i].ID, documentNo = Model.LoanDeductions[i].DocumentNo }, new { style = @Model.AddEditDeleteDeductionStyle })</td>*@
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                    <p></p>
                </div>
            </div>
        </div>

        <div id="divPaymentSchedule">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-left">
                        <h3>Payment Schedule</h3>
                    </div>
                    <div class="pull-right">
                        <h3>Due to date: @Html.ValueFor(m => m.Loan.DueToDate, "{0:#,###,##0.00}")</h3>
                    </div>
                </div>
                <div class="ibox-title hidden">
                    <table>
                        <tr>
                            <td style="@Model.GenerateScheduleStyle"><a href="@Url.Action("CreatePaymentSchedule", "LoanSchedule", new { documentNo = Model.Loan.DocumentNo, command = "CreateSchedule" })" class="btn btn-primary btn-sm">Generate payment schedule</a></td>
                            <td style="@Model.DeleteScheduleStyle"><a href="@Url.Action("GeneratePaymentSchedule", "LoanSchedule", new { documentNo = Model.Loan.DocumentNo, command = "DeleteSchedule" })" class="btn btn-primary btn-sm">Delete payment schedule</a></td>
                        </tr>
                    </table>
                </div>
                <div class="ibox-content">
                    <div class="divScrollDeductions">
                        <table class="table table-bordered">
                            <thead class="k-grid-header">
                                <tr>
                                    <th></th>
                                    <th><span>Due Date</span></th>
                                    <th>Day of Week</th>
                                    <th>Cheque Details</th>
                                    <th><span>Due Amount</span></th>
                                </tr>
                            </thead>
                            <tbody>
                                @for (var i = 0; i < Model.LoanSchedule.Count(); i++)
                            {
                                var item = i + 1;
                                    <tr>
                                        <td>@item</td>
                                        <td>@Html.ValueFor(m => Model.LoanSchedule[i].DueDate, "{0:MMM/dd/yyyy}")</td>
                                        <td>@Html.ValueFor(m => Model.LoanSchedule[i].DueDate.DayOfWeek)</td>
                                        <td>@Html.ValueFor(m => Model.LoanSchedule[i].ChequeDetails)</td>
                                        <td style="text-align: right">@Html.ValueFor(m => Model.LoanSchedule[i].DueAmount, "{0:#,###,##0.00}")</td>
                                    </tr>
                            }
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

        <div id="divPayments" class="col-lg-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <div class="pull-left">
                        <h3>Payments</h3>
                    </div>
                    <div class="pull-right">
                        <h3>
                            Total penalty: @Html.ValueFor(m => m.Loan.TotalPenalty, "{0:#,###,##0.00}") 
                            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                            Total payment: @Html.ValueFor(m => m.Loan.TotalPayment, "{0:#,###,##0.00}")</h3>
                    </div>
                </div>
                @if (Model.IsAdmin && Model.Loan.StatusID == (int)AdministrationHelper.LoanStatus.ReleasedToBorrower)
                {
                <div class="ibox-title">
                    @Html.ActionLink("Add Payment", "PaymentAddEdit", "Loan", new { documentNo = Model.Loan.DocumentNo }, new { @class = "btn btn-primary" })
                </div>
                }
                <div class="ibox-content">
                    <div class="divScrollPayments">
                        <table class="table table-bordered">
                            <thead class="k-grid-header">
                                <tr>
                                    <th><span>Payment Date</span></th>
                                    <th><span>Payment Type</span></th>
                                    <th><span>Penalty</span></th>
                                    <th><span>Amount</span></th>
                                    <th><span>Is Void</span></th>
                                    <th><span>Void Date</span></th>
                                    <th><span>Void By</span></th>
                                    <th><span>Remarks</span></th>
                                    <th></th>
                                </tr>
                            </thead>
                            <tbody>
                                @for (var i = 0; i < Model.LoanPayments.Count(); i++)
                                {
                                <tr>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].PaymentDate, "{0:MMM/dd/yyyy}")</td>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].PaymentTypeName)</td>
                                    <td style="text-align:right">@Html.ValueFor(m => Model.LoanPayments[i].Penalty, "{0:#,###,##0.00}")</td>
                                    <td style="text-align:right">@Html.ValueFor(m => Model.LoanPayments[i].Amount, "{0:#,###,##0.00}")</td>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].IsVoid)</td>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].VoidDate, "{0:MMM/dd/yyyy}")</td>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].VoidBy)</td>
                                    <td>@Html.ValueFor(m => Model.LoanPayments[i].Remarks)</td>
                                    <td>
                                        @if (Model.IsAdmin && Model.LoanPayments[i].StatusID == (int)AdministrationHelper.LoanPaymentStatus.New
                                            && Model.Loan.StatusID == (int)AdministrationHelper.LoanStatus.ReleasedToBorrower)
                                        {
                                            @Html.ActionLink("Edit", "PaymentAddEdit", "Loan", new { id = Model.LoanPayments[i].ID, documentNo = Model.Loan.DocumentNo }, null)
                                        }
                                    </td>
                                </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                </div>


            </div>
        </div>

    </div>
</div>
@section Styles {
    @Styles.Render("~/Content/plugins/dataTables/dataTablesStyles")

    <style>

        .divScrollDeductions {
            height: 335px;
            overflow-y: scroll;
            border: solid 1px #EBEBEB;
        }

        .divScrollPayments {
            height: 382px;
            overflow-y: scroll;
            border: solid 1px #EBEBEB;
        }

    </style>
}
@section Scripts {

    <script type="text/javascript">

        function SaveDetails()
        {
            var documentNo = $("#txtDocumentNo").val();
            var paymentTypeID = $("#ddlPaymentType").val();

            $.ajax({
                url: '/Loan/SaveDetails',
                type: "GET",
                dataType: "JSON",
                data: { documentNo: documentNo, paymentTypeID: paymentTypeID },
                success: function () { }
            });
        }

    </script>
}
